1  Python Basics

1.1 Values and Variables

1.1.1 - Values

type("Hello, World")
str
type(())
tuple
type([])
list
type(set())
set
type({})
dict
# 파이썬에서는 복소수 i를 j로 표현함
type(2+3j)
complex

1.1.2 - Variables

message="Hello, world!"
n=42
e=2.71
print(n)
print(message)
42
Hello, world!
length=4.2
height=3.5
area=length*height
print(area)
14.700000000000001
# 변수는 방문앞에 이름만 바꾼것... 새로운 변수에 기존 변수를 할당하고, 기존 변수를 변경하면 새로운 변수도 영향받음
a=[1,2,3]
b=a
a[0]=10
print(b)
[10, 2, 3]

1.1.3 - Variable names

  • 숫자로 시작하면 안됌
x=1.0
X=1.0
X1=1.0
x1=1.0
dell=1.0
# not allowed
# x:, 1X, X1-1, for
x,y,z=1,3.14,'a'
print(x,y,z)
1 3.14 a

1.2 Core Data Types

1.2.1 - Floating Point(float)

x=1
y=1.0
z=float(1)
xx=1.234e-5
print(x,y,z,xx)
print(type(z),type(float(True)))
1 1.0 1.0 1.234e-05
<class 'float'> <class 'float'>
# float 타입은 근사치를 사용하기때문에 사용에 유의
1-0.9==0.1
False
float('inf')
inf

1.2.2 - Complex

x=1j
y=2+3j
z=complex(1)
print(x,y,z,y.real,y.imag)
print(type(y.imag))
1j (2+3j) (1+0j) 2.0 3.0
<class 'float'>

1.2.3 - Integer

# Integer는 float와 달리 정확하다
x=2**127+2**65 
x
170141183460469231768580791863303208960

1.2.4 - Boolean(bool)

print(bool(1),bool(1.2),bool(-1),bool(0.1))
True True True True
print(bool(0),bool(0.0),bool(0.0000),bool(None),bool(""),bool(()))
False False False False False False
3>4
False

1.3 Operators

1.3.1 - Arithmetic Operators

a=10
b=20
# 나누기 연산은 항상 float로 반환됨을 유의
print(a+b,a-b,a*b,b/a)
30 -10 200 2.0
# 나머지 연산자
b%a
0
# 몫 연산자
b//a
2
# 거듭제곱 연산자, 우선순위가 가장 높음
a**b
100000000000000000000

1.3.2 - Relational Operators

print(a==b,a!=b,a>b,a<b,a>=b,a<=b)
False True False True False True

1.3.3 - Assignment Operators

t=0
t+=1
t
1
t/=2
t
0.5
t*=2
t
1.0

1.3.4 - Logical Operators

print(True and False, True or False, not True)
False True False
# 논리연산자는 container가 bool이 아닌 경우 용법이 다름
# or은 가장 먼저나오는 True값을 반환, and는 가장 먼저나오는 false값을 반환
print('' or 'abc',0 or 1, 'abc' or '','abc' or 'ab')
print(1 and 0, 'abc' and 0, None and '' and 0)
abc 1 abc abc
0 0 None

1.4 Python Operators Precedence

1.4.1 - Python Operators Precedence

# 거듭제곱 > 덧샘뺄샘 > 나눗셈, 몫, 나머지 > 부등호 > 논리
1+3/2
2.5
2400//500*500+2000%500
2000
(2400//500)*500+(2000%500)
2000

1.5 Mathmatical Function

# built-in
print(abs(-1),round(1.111,1))
1 1.1
# functional in math module
import math

# factorial, floor, isfinite, isinf, isnan, trunc
# exp, log, log10, sqrt, cos, sin, tan, degrees, radians, pi, e

print(
math.ceil(1.11),
math.log(10),
math.log(10,10),
math.exp(1),
math.e,
math.pi,
math.trunc(math.pi),
math.degrees(1),
math.cos(math.pi)
)
2 2.302585092994046 1.0 2.718281828459045 2.718281828459045 3.141592653589793 3 57.29577951308232 -1.0

1.6 Python I/O

1.6.1 - Python Output

# print(object1, object2, ... , sep=' ', end='\n',...)
print(1,2,3,4)
print(1,2,3,4,sep='*')
print(1,2,3,4,end='END')
print(1,2,3,4,sep='')
1 2 3 4
1*2*3*4
1 2 3 4END1234
# formatting
x=5; y=10
print('The value of x is {} and y is {}'.format(x,y))
print('I love {2} and {1}, but hate {0}'.format('cucumber','butter','bread'))
The value of x is 5 and y is 10
I love bread and butter, but hate cucumber

1.6.2 - Python Input

# Input value is a string
num=input('Enter a number:')
print(num)
print(int(num))
10
10